package com.kim.nidoya.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.kim.androidbase.db.DBCommandInteface;
import com.kim.androidbase.db.DBSession;
import com.kim.androidbase.db.IDBUtils.QueryCompleteCallback;
import com.kim.androidbase.db.SQLiteCriteria;
import com.kim.nidoya.domain.User;
import com.kim.nidoya.domain.constant.UserConstant;

public class UserDBCommand implements DBCommandInteface<User, String> {

	DBSession dbSession;

	public UserDBCommand(Context context) {
		dbSession = new DBSession(context, new DBHelper(context/*
																 * ,
																 * NidoyaApplication
																 * .
																 * databaseErrorHandler
																 */));
	}

	@Override
	public void add(User t) {
		dbSession.insert(UserConstant.TABLENAME, getContentValues(t),
				SQLiteDatabase.CONFLICT_ABORT);
	}

	@Override
	public void add(List<User> ts) {
		List<ContentValues> contentValuesList = new ArrayList<ContentValues>();
		for (User user : ts) {
			contentValuesList.add(getContentValues(user));
		}
		dbSession.insertBatch(UserConstant.TABLENAME, contentValuesList,
				SQLiteDatabase.CONFLICT_ABORT);
	}

	@Override
	public void delete(String pk) {
		dbSession.delete(UserConstant.TABLENAME, UserConstant.USERID + "=?",
				new String[] { pk });
	}

	@Override
	public void delete(SQLiteCriteria sqLiteCriteria) {
		dbSession.delete(sqLiteCriteria.getTableName(),
				sqLiteCriteria.getWhereClause(), sqLiteCriteria.getWhereArgs());
	}

	@Override
	public void modify(SQLiteCriteria sqLiteCriteria) {
		dbSession.modify(sqLiteCriteria.getTableName(),
				sqLiteCriteria.getContentValues(),
				sqLiteCriteria.getWhereClause(), sqLiteCriteria.getWhereArgs());
	}

	@Override
	public void modify(String pk, SQLiteCriteria sqLiteCriteria) {
		dbSession.modify(sqLiteCriteria.getTableName(),
				sqLiteCriteria.getContentValues(),
				sqLiteCriteria.getWhereClause(), new String[] { pk });
	}

	@Override
	public User query(String pk) {
		final User user = new User();
		dbSession.query("select * from " + UserConstant.TABLENAME + " where "
				+ UserConstant.USERID + "=?", new String[] { pk },
				new QueryCompleteCallback() {

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor == null || cursor.getCount() == 0) {
							return;
						}
						if (cursor.moveToNext()) {
							setUserProperty(user, cursor);
						}
					}

					@Override
					public void onQueryError(String errorMsg) {
						// TODO Auto-generated method stub

					}

				});
		return user;
	}

	public User queryCurrentUser() {
		final User user = new User();
		dbSession.query("select * from " + UserConstant.TABLENAME, null,
				new QueryCompleteCallback() {

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor == null || cursor.getCount() == 0) {
							return;
						}
						if (cursor.moveToNext()) {
							setUserProperty(user, cursor);
						}
					}

					@Override
					public void onQueryError(String errorMsg) {
						// TODO Auto-generated method stub

					}

				});
		return user;
	}

	@Override
	public List<User> query(SQLiteCriteria sqLiteCriteria) {
		return null;
	}

	private void setUserProperty(User user, Cursor cursor) {
		// user.setUserID(dbSession.getString(cursor, UserConstant.USERID));
		// user.setUserName(dbSession.getString(cursor, UserConstant.USERNAME));
		// user.setUserPassword(dbSession.getString(cursor,
		// UserConstant.USERPASSWORD));
		// user.setAge(dbSession.getInt(cursor, UserConstant.AGE));
		// user.setGender(dbSession.getString(cursor, UserConstant.GENDER));
		// user.setHeadIcon(dbSession.getString(cursor, UserConstant.HEADICON));
	}

	private ContentValues getContentValues(User user) {
		// ContentValues contentValues = new ContentValues();
		// contentValues.put(UserConstant.USERID, user.getUserID());
		// contentValues.put(UserConstant.USERNAME, user.getUserName());
		// contentValues.put(UserConstant.USERPASSWORD, user.getUserPassword());
		// contentValues.put(UserConstant.AGE, user.getAge());
		// contentValues.put(UserConstant.GENDER, user.getGender());
		// contentValues.put(UserConstant.HEADICON, user.getHeadIcon());
		// return contentValues;
		return null;
	}

	@Override
	public List<User> queryAll() {
		return null;
	}

}
